package Com.HupochuanAdmin.Uil.Do;

import java.util.Hashtable;
import java.util.List;

import Com.Hupochuan.Uil.Servlet.DoRunServlet;
import Com.Hupochuan.Vo.Area;
import Com.Hupochuan.Vo.AreaSort;
import Com.Hupochuan.Vo.Movie;
import Com.Hupochuan.Vo.MovieActor;
import Com.Hupochuan.Vo.MovieSource;
import Com.Hupochuan.Vo.MovieType;
import Com.Hupochuan.Vo.SourceLanguage;
import Com.Hupochuan.Vo.SourceMode;
import Com.Hupochuan.Vo.SourceSubtitle;
import Com.Hupochuan.Vo.SourceType;
import Com.Hupochuan.Vo.Type;
import Com.HupochuanAdmin.Dal.Tab.AreaSortTab;
import Com.HupochuanAdmin.Dal.Tab.AreaTab;
import Com.HupochuanAdmin.Dal.Tab.MovieActorTab;
import Com.HupochuanAdmin.Dal.Tab.MovieSourceTab;
import Com.HupochuanAdmin.Dal.Tab.MovieTab;
import Com.HupochuanAdmin.Dal.Tab.MovieTypeTab;
import Com.HupochuanAdmin.Dal.Tab.SourceLanguageTab;
import Com.HupochuanAdmin.Dal.Tab.SourceModeTab;
import Com.HupochuanAdmin.Dal.Tab.SourceSubtitleTab;
import Com.HupochuanAdmin.Dal.Tab.SourceTypeTab;
import Com.HupochuanAdmin.Dal.Tab.TypeTab;
import Com.HupochuanAdmin.Uil.Util.AdminParameter;
import Com.Tester.Tester;

@SuppressWarnings("serial")
public class MovieListDo extends DoRunServlet{
	public void doRun(){
		
		//打印测试
		Tester.print("打印测试 - MovieListDo");
		
		//获得数据
		String searchType = this.getDoTool().getUrlChinesePara("searchType");
		String searchKey = this.getDoTool().getUrlChinesePara("searchKey");
		long typeId = this.getDoTool().getLongPara("typeId");
		long areaSortId = this.getDoTool().getLongPara("areaSortId");
		long areaId = this.getDoTool().getLongPara("areaId");
		String state = this.getDoTool().getPara("state");
		String showRow = this.getDoTool().getPara("showRow");
		long pageNo = this.getDoTool().getLongPara("pageNo");
		
		//获得内存参数
		long pageSize = AdminParameter.getObject().getMovieListPageSize();
		String order = AdminParameter.getObject().getMovieListOrder();
		boolean asc = AdminParameter.getObject().isMovieListAsc();
		Hashtable<String,Boolean> showCol = AdminParameter.getObject().getMovieListShowCol();
		
		long infoCount = MovieTab.getObject().findMovieCount(searchType,searchKey,typeId,areaSortId,showRow,areaId);
		
		//初始化页数
		long pageCount = 1;
		
		//计算页数
		if(infoCount != 0){
			//如果信息数量正好满页
			if(infoCount % pageSize == 0){
				pageCount = infoCount / pageSize; 
			}
			//如果信息数量不满页
			else{
				pageCount = infoCount / pageSize + 1;
			}
		}
		
		if(pageNo > pageCount){
			pageNo = pageCount;
		}
		else if(pageNo < 1){
			pageNo = 1;
		}
		
		//获得类型列表
		List<Type> typeList = TypeTab.getObject().findTypeList();
		
		//获得地区分类列表
		List<AreaSort> areaSortList = AreaSortTab.getObject().findAreaSortList();
		
		//获得地区列表
		List<Area> areaList = AreaTab.getObject().findAreaList(areaSortId);
		
		//获得电影
		List<Movie> movieList = MovieTab.getObject().findMovieList(searchType,searchKey,typeId,areaSortId,showRow,areaId,order,asc,pageNo,pageSize);
		
		//如果要 显示类型列表
		if(showCol.get("showBaikeInfo") && showCol.get("showType")){
			//遍历电影列表 获得每个电影的类型列表
			for(Movie movie : movieList){
				List<MovieType> movieTypeList = MovieTypeTab.getObject().findMovieTypeList(movie.getId());
				movie.setMovieTypeList(movieTypeList);
			}
		}
		
		//如果要 显示演员列表
		if(showCol.get("showDirectorInfo") && showCol.get("showActor")){
			//遍历电影列表 获得每个电影的演员列表
			for(Movie movie : movieList){
				List<MovieActor> movieActorList = MovieActorTab.getObject().findMovieActorListByMovieId(movie.getId());
				movie.setMovieActorList(movieActorList);
			}
		}
		
		List<SourceType> sourceTypeList = null;
		List<SourceLanguage> sourceLanguageList = null;
		List<SourceSubtitle> sourceSubtitleList = null;
		List<SourceMode> sourceModeList = null;
		
		//如果要 显示资源列表
		if(showCol.get("showSourceInfo") && showCol.get("showSource")){
			//遍历电影列表 获得每个电影的资源列表
			for(Movie movie : movieList){
				List<MovieSource> movieSourceList = MovieSourceTab.getObject().findMovieSourceList(movie.getId());
				movie.setMovieSourceList(movieSourceList);
			}
			
			//获得资源类型列表
			sourceTypeList = SourceTypeTab.getObject().findSourceTypeList();
			
			//获得资源语言列表
			sourceLanguageList = SourceLanguageTab.getObject().findSourceLanguageList();
			
			//获得资源字幕列表
			sourceSubtitleList = SourceSubtitleTab.getObject().findSourceSubtitleList();
			
			//获得资源状况列表
			sourceModeList = SourceModeTab.getObject().findSourceModeList();
			
		}
		
		//设置数据
		this.getDoTool().setAttr("searchType", searchType);
		this.getDoTool().setAttr("searchKey", searchKey);
		this.getDoTool().setAttr("typeId", typeId);
		this.getDoTool().setAttr("areaSortId", areaSortId);
		this.getDoTool().setAttr("areaId", areaId);
		this.getDoTool().setAttr("state", state);
		this.getDoTool().setAttr("showRow", showRow);
		this.getDoTool().setAttr("order", order);
		this.getDoTool().setAttr("asc", asc);
		this.getDoTool().setAttr("infoCount",infoCount);
		this.getDoTool().setAttr("pageSize", pageSize);
		this.getDoTool().setAttr("pageNo", pageNo);
		this.getDoTool().setAttr("pageCount", pageCount);
		this.getDoTool().setAttr("typeList", typeList);
		this.getDoTool().setAttr("areaSortList", areaSortList);
		this.getDoTool().setAttr("areaList", areaList);
		this.getDoTool().setAttr("movieList", movieList);
		this.getDoTool().setAttr("showCol", showCol);
		this.getDoTool().setAttr("sourceTypeList", sourceTypeList);
		this.getDoTool().setAttr("sourceLanguageList", sourceLanguageList);
		this.getDoTool().setAttr("sourceSubtitleList", sourceSubtitleList);
		this.getDoTool().setAttr("sourceModeList", sourceModeList);
		
		//转发jsp页面
		this.getDoTool().forward("/jsp/admin/movieList.jsp");
	}
}
